{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b1746d15",
   "metadata": {},
   "source": [
    "# 环境验证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a1cf2e40",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-18T04:53:41.500680Z",
     "start_time": "2023-09-18T04:53:39.522155Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0.0+cu118\n",
      "True\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "# 测试torch\n",
    "print(torch.__version__)\n",
    "\n",
    "# 判断是否安装了cuda\n",
    "print(torch.cuda.is_available())  \n",
    "# 返回True则说明已经安装了cuda\n",
    "\n",
    "# 判断是否安装了cuDNN\n",
    "from torch.backends import cudnn\n",
    "\n",
    "print(cudnn.is_available())  \n",
    "# 返回True则说明已经安装了cuDNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bc30d94",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试tensorflow\n",
    "import tensorflow as tf\n",
    "print(tf.__version__)\n",
    "print(tf.test.is_gpu_available())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd23fade",
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.config.list_physical_devices('GPU')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5f23373",
   "metadata": {},
   "source": [
    "# 模型部署"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "360d8cf4",
   "metadata": {},
   "source": [
    "\n",
    "- 轻量化网络需要进行嵌入式部署验证，目前使用ncnn\n",
    "\n",
    "## 模型转换\n",
    "\n",
    "\n",
    "- 参考链接 [torchscript-to-pnnx](https://zhuanlan.zhihu.com/p/427512763)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcf621b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# func: pth2pt\n",
    "# pth 不包含网络模型，只保存权重参数\n",
    "# pt  包含网络模型和参数\n",
    "import torch\n",
    "\n",
    "# net = models.mobilenet_v2(pretrained=True)\n",
    "# 需要使用加载权重的网络模型\n",
    "net = net.eval()\n",
    "\n",
    "# 输入模型\n",
    "x = torch.rand(1, 3, 224, 224)\n",
    "\n",
    "# 使用jit工具转换\n",
    "module = torch.jit.trace(net, x)\n",
    "torch.jit.save(module, \"model.pt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc6831e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 在项目中使用(以yolov5s_mod为例)：\n",
    "# 追加本cell到项目根目录 yolo.py 尾部\n",
    "if __name__ == \"__main__\":\n",
    "\n",
    "    # 参数设定\n",
    "    model_path = \"logs_v5s_2h/ep600-loss0.019-val_loss0.019.pth\"\n",
    "    anchors_mask = [[3, 4, 5], [0, 1, 2]]\n",
    "    num_classes = 1\n",
    "    phi = \"s\"\n",
    "    backbone = \"cspdarknet\"\n",
    "    input_shape = [352, 640]\n",
    "\n",
    "    # 参考 generate()\n",
    "    net = YoloBody(anchors_mask, num_classes, phi, backbone, input_shape= input_shape,pretrained=False)\n",
    "    net.load_state_dict(torch.load(model_path))\n",
    "    net = net.eval()\n",
    "\n",
    "    # 输入模型 BCHW 格式\n",
    "    x = torch.rand(1, 3, 224, 224)\n",
    "\n",
    "    # 使用jit工具转换\n",
    "    module = torch.jit.trace(net, x)\n",
    "    torch.jit.save(module, \"model.pt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "abad9612",
   "metadata": {},
   "outputs": [],
   "source": [
    "# func: pt2ncnn\n",
    "# 使用编译好的pnnx直接在pt目录生成ncnn与pnnx\n",
    "# ../pnnx src_git/yolov4/model_data/lite_model.pt \"inputshape=[1,3,352,640]\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "736f9d3e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
